/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.news.server.check;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author zwlin
 */
public class CheckDesc {

    public static enum CheckType {
        Base64, Domain, Email, IntegerNonZeroRange, IntegerZeroRange, Ip, Long, MD5, Phone, PlatInfo, QQ, TimeDelay, TimeLater, Enums;
    }

    public static CheckDesc parse(String sDesc) {
        if (sDesc == null) {
            return null;
        }
        sDesc = sDesc.trim();
        if (sDesc.isEmpty()) {
            return null;
        }
        CheckType type = null;
        Object obj = null;
        switch (sDesc.charAt(0)) {
            case 'b'://Base64
                type = CheckType.Base64;
                break;
            case 'd'://Domain
                type = CheckType.Domain;
                break;
            case 'e':
                if (sDesc.length() > 1) {
                    switch (sDesc.charAt(1)) {
                        case 'n'://enums
                            type = CheckType.Enums;
                            String[] e = sDesc.substring(2).split("\\|");
                            List em = Arrays.asList(e);
                            List arrayList = new ArrayList(em);
                            if (sDesc.substring(2).endsWith("\\|")) {
                                arrayList.add("");
                            }
                            obj = arrayList;
                            break;
                    }
                } else {
                    type = CheckType.Email;
                    break;
                }
                break;
            case 'z'://ZeroIntegerRange
                type = CheckType.IntegerZeroRange;
                obj = Long.parseLong(sDesc.substring(1));
                break;
            case 'n'://NonZeroIntegerRange
                type = CheckType.IntegerNonZeroRange;
                obj = Long.parseLong(sDesc.substring(1));
                break;
            case 'i'://Ip
                type = CheckType.Ip;
                break;
            case 'l'://Long
                type = CheckType.Long;
                break;
            case 'm'://MD5
                if (sDesc.length() < 2) {
                    return null;
                }
                type = CheckType.MD5;
                obj = sDesc.substring(1);
                break;
            case 'p':
                switch (sDesc.charAt(1)) {
                    case 'h'://Phone
                        type = CheckType.Phone;
                        break;
                    case 'l'://PlatInfo
                        type = CheckType.PlatInfo;
                        break;
                }
                break;
            case 'q':
                type = CheckType.QQ;
                break;
            case 't'://TimeXXX
                if (sDesc.length() < 3) {
                    return null;
                }
                switch (sDesc.charAt(1)) {
                    case 'd'://TimeDelay
                        type = CheckType.TimeDelay;
                        try {
                            obj = Integer.parseInt(sDesc.substring(2));
                        } catch (NumberFormatException e) {
                            return null;
                        }
                        break;
                    case 'l'://TimeLater
                        type = CheckType.TimeLater;
                        obj = sDesc.substring(2);
                        break;
                }
                break;
            default:
                return null;
        }
        return new CheckDesc(type, obj);
    }

    public final CheckType type;
    public final Object param;

    public CheckDesc(CheckType type, Object param) {
        this.type = type;
        this.param = param;
    }

    public static String enumToString(CheckDesc desc) {
        String name = null;
        switch (desc.type) {
            case Base64:
                name = "Base64+" + desc.param;
                break;
            case Domain:
                name = "Domain+" + desc.param;
                break;
            case Email:
                name = "Email+" + desc.param;
                break;
            case Enums:
                name = "Enums+" + desc.param;
                break;
            case IntegerNonZeroRange:
                name = "IntegerNonZeroRange+" + desc.param;
                break;
            case IntegerZeroRange:
                name = "IntegerZeroRange+" + desc.param;
                break;
            case Ip:
                name = "Ip+" + desc.param;
                break;
            case Long:
                name = "Long+" + desc.param;
                break;
            case MD5:
                name = "MD5+" + desc.param;
                break;
            case Phone:
                name = "Phone+" + desc.param;
                break;
            case PlatInfo:
                name = "PlatInfo+" + desc.param;
                break;
            case QQ:
                name = "QQ+" + desc.param;
                break;
            case TimeDelay:
                name = "TimeDelay+" + desc.param;
                break;
            case TimeLater:
                name = "TimeLater+" + desc.param;
                break;
        }
        return name;
    }

}
